package com.service.scheduling.task;


import com.service.dao.constants.ConfigConstants;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.model.CoinsPrice;
import com.service.dao.model.SysConfig;
import com.service.dao.model.TradingSpotOrder;
import com.service.scheduling.dto.SpotTradePriceVo;
import com.service.scheduling.service.CoinsPriceService;
import com.service.scheduling.service.ConfigService;
import com.service.scheduling.service.TradingSpotOrderService;
import com.service.scheduling.service.TradingSpotTradesService;
import com.service.scheduling.utils.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Slf4j
@Component
public class TradingSpotTradeTask {

    @Autowired
    private CoinsPriceService coinsPriceService;

    @Autowired
    private TradingSpotOrderService spotOrderService;

    @Autowired
    private TradingSpotTradesService spotTradesService;

    @Autowired
    private ConfigService configService;

//    @Scheduled(cron = "0 1 0 * * ?")
    public void doScheduled() {
        log.info("TradingSpotTradeTask-doScheduled start");
        Date today = new Date();
        Date yesterday = DateUtils.addDays(today, -1);
        String yesterdayStr = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
//        Map<String, SysConfig> configMap = configService.mapByConfigGroup(ConfigConstants.otcPriceGroup());

//        CoinsPrice coinsPrice = coinsPriceService.getLastCoinsPrice(CoinsEnum.EXDF.getCode());

        //TODO:读取交易所的价格
//        SpotTradePriceVo tradePriceVo = spotTradesService.getTradePrice(CoinsEnum.EXDF.getName(), yesterday);
//        BigDecimal maxOutPrice = BigDecimal.ZERO;
//        BigDecimal minOutPrice = BigDecimal.ZERO;
//        if (!ObjectUtils.isEmpty(tradePriceVo)) {
//            maxOutPrice = tradePriceVo.getMaxPrice();
//            minOutPrice = tradePriceVo.getMinPrice();
//        }

//        BigDecimal standOutPrice = MathUtil.div(maxOutPrice.add(minOutPrice), BigDecimal.valueOf(2));
//                .add(getFloatNum(MathUtil.parseBigDecimal(configMap.get(ConfigConstants.EXDF_FLOAT_PRICE).getConfValue())));

//        BigDecimal maxStandPrice = getStandardPrice(configMap, false);
//        BigDecimal minStandPrice = getStandardPrice(configMap, true);

//        BigDecimal standPrice = standOutPrice;
//        log.info("日期:{},平均价格:{},最大价格:{},配置最大价格:{},最小价格:{},配置最小价格:{}", yesterdayStr, standOutPrice,
//                maxOutPrice, maxStandPrice, minOutPrice, minStandPrice);

//        if (standOutPrice.compareTo(maxStandPrice) > 0) {
//            standPrice = maxStandPrice;
//        } else if (standOutPrice.compareTo(minOutPrice) < 0) {
//            standPrice = minStandPrice;
//        }
//
//        coinsPriceService.save(CoinsPrice.builder()
//                .coinsId(coinsPrice.getCoinsId())
//                .coinsName(coinsPrice.getCoinsName())
//                .coinPrice(standOutPrice)
//                .maxPrice(maxStandPrice)
//                .minPrice(minStandPrice)
//                .standardPrice(standPrice)
//                .createTime(today)
//                .build());
        log.info("ExchangeTask-doScheduled end");
    }

    private BigDecimal getStandardPrice(Map<String, SysConfig> configMap, Boolean isMin) {
        String baseName = ConfigConstants.EXDF_MAX_BASE_PRICE;
        String floatName = ConfigConstants.EXDF_MAX_FLOAT_PRICE;
        if (isMin) {
            baseName = ConfigConstants.EXDF_MIN_BASE_PRICE;
            floatName = ConfigConstants.EXDF_MIN_FLOAT_PRICE;
        }

        BigDecimal basePrice = MathUtil.parseBigDecimal(configMap.get(baseName).getConfValue());
        BigDecimal floatPrice = MathUtil.parseBigDecimal(configMap.get(floatName).getConfValue());
        return basePrice.add(getFloatNum(floatPrice));
    }

    private BigDecimal getFloatNum(BigDecimal floatNum) {
        return BigDecimal.valueOf(RandomUtils.nextDouble(0, floatNum.doubleValue()));
    }

}
